<template>
  <!-- login的大盒子 -->
  <div class="divLogin">
    <!-- 左面的盒子 -->
    <div class="divLeft">
      <!-- title图片 -->
      <div class="divTitle"><img src="http://www-wms-java.itheima.net/img/title.ef005a7a.png" alt=""></div>
      <!-- 表单 -->
      <el-form ref="loginForm" :model="loginForm" label-width="50px" :rules="loginRules" style="margin-top: 50px; margin-left: 10px">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <div class="divImg"><img src="../../assets/icon/phone.svg" alt=""></div>
          <el-input v-model="loginForm.username" class="input" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <div class="divImg"><img src="../../assets/icon/lock.svg" alt=""></div>
          <el-input ref="password" v-model="loginForm.password" class="input" :type="passwordType" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!-- 记住密码 -->
        <el-form-item>
          <!-- `checked` 为 true 或 false -->
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </el-form-item>
        <!-- 点击登陆 -->
        <el-form-item>
          <el-button :loading="loading" class="btn" @click="loginFn">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 右面的盒子 -->
    <div class="divRight" />
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '必填' }],
        password: [{ required: true, trigger: 'blur', message: '必填' }]
      },
      // 记住密码的状态
      checked: false,
      // 查看密码
      passwordType: 'password',
      // 禁用按钮
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 小眼睛查看密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登陆
    loginFn() {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        this.loading = true
        await this.$store.dispatch('user/login')
        this.$router.push({ path: this.redirect || '/' })
        this.loading = false
      })
    },
    // 占位函数
    fn() {
      validUsername()
    }
  }
}
</script>

<style lang="scss" scoped>
.divLogin {
  width: 958px;
  height: 516px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 20px rgb(93 93 93 / 33%);
  background-color: #fff;
  border-radius: 40px;
}
.divLeft {
  float: left;
  width: 398px;
  height: 516px;
  background: #fff;
  border-radius: 34px 0 0 34px;
  margin: 0 auto;
  .divTitle {
    margin-left: 124px;
    margin-top: 72px;
    img {
      width: 150px;
      height: 64px;
    }
  }
  ::v-deep .el-form-item__content {
    display: flex;
  }
  ::v-deep .el-input__inner {
    width: 230px;
    height: 50px;
    background-color: #f8f5f5;
    border-bottom-left-radius: 0;
    border-top-left-radius: 0;
  }
  ::v-deep .divImg {
    display: inline-block;
    width: 50px;
    height: 50px;
    background: #eae7e7;
    border-radius: 8px 0 0 8px;
    width: 50px;
    height: 50px;
    img {
      width: 25px;
      height: 24px;
      margin: 12px 13px;
      color: #fff;
    }
  }

  .btn {
    width: 280px;
    height: 50px;
    background: #ffb200;
    border-radius: 8px;
    box-shadow: 0 2px 9px 1px rgb(255 178 0 / 47%);
    font-size: 16px;
    font-weight: 600;
    text-align: center;
    color: #332929;
    line-height: 22px;
  }
}
.divRight {
  float: right;
  background-image: url('http://www-wms-java.itheima.net/img/contentBg.1321d126.png');
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  width: 560px;
  height: 516px;
}
.show-pwd {
  position: absolute;
  right: 70px;
  top: 7px;
  font-size: 16px;
  color: #889aa4;
  cursor: pointer;
  user-select: none;
}
</style>
